我們寫到這邊已經建立了不少的JSON檔,這些JSON通常都是用來定義一些合成表、掉落物以及模型等等的內容。其實這些JSON檔的重複性很高,是可以靠著程式進行自動生成的,使用自動生成的JSON檔可以讓我們不需要再花時間在手寫JSON檔上,這樣之後製作樓梯、告示牌等物品會方便很多。
Data Generation需要單獨一個資料夾,Java/net/como/datagen,我們需要五個class,分別是ModRecipeProvider、ModModelProvider、ModLootTableProvider、ModItemTagProvider跟ModBlockTagProvider,這幾個class分別要繼承相對應的FabricProvider
ModRecipeProvider繼承FabricRecipeProvider
ModLootTableProvider繼承FabricBlockProvider
ModModelProvider繼承FabricModelProvider
ModBlockTagProvider繼承FabricTagProvider.BlockTagProvider
ModItemTagProvider繼承FabricTagProvider.ItemTagProvider
我們先說ModRecipeProvider
public class ModRecipeProvider extends FabricRecipeProvider {
public ModRecipeProvider(FabricDataOutput output) {
super(output);
}
@Override
public void generate(Consumer<RecipeJsonProvider> exporter) {
offerReversibleCompactingRecipes(exporter, RecipeCategory.BUILDING_BLOCKS, Moditems.Siderite, RecipeCategory.MISC, ModBlocks.SIDERITE_BLOCK);
offerSlabRecipe(exporter, RecipeCategory.BUILDING_BLOCKS,ModBlocks.SIDERITE_SLAB,Moditems.Siderite);
offerWallRecipe(exporter, RecipeCategory.BUILDING_BLOCKS,ModBlocks.SIDERITE_WALL,Moditems.Siderite );
offerPressurePlateRecipe(exporter, ModBlocks.SIDERITE_PRESSURE_PLATE, Moditems.Siderite);
createDoorRecipe(ModBlocks.SIDERITE_DOOR, Ingredient.ofItems(Moditems.Siderite)).criterion(hasItem(Moditems.Siderite),conditionsFromItem(Moditems.Siderite)).offerTo(exporter, new Identifier(getRecipeName(ModBlocks.SIDERITE_DOOR)));
createFenceRecipe(ModBlocks.SIDERITE_FENCE, Ingredient.ofItems(Moditems.Siderite)).criterion(hasItem(Moditems.Siderite),conditionsFromItem(Moditems.Siderite)).offerTo(exporter, new Identifier(getRecipeName(ModBlocks.SIDERITE_FENCE)));
createFenceGateRecipe(ModBlocks.SIDERITE_FENCE_GATE, Ingredient.ofItems(Moditems.Siderite)).criterion(hasItem(Moditems.Siderite),conditionsFromItem(Moditems.Siderite)).offerTo(exporter, new Identifier(getRecipeName(ModBlocks.SIDERITE_FENCE_GATE)));
createStairsRecipe(ModBlocks.SIDERITE_STAIRS, Ingredient.ofItems(Moditems.Siderite)).criterion(hasItem(Moditems.Siderite),conditionsFromItem(Moditems.Siderite)).offerTo(exporter, new Identifier(getRecipeName(ModBlocks.SIDERITE_STAIRS)));
createTrapdoorRecipe(ModBlocks.SIDERITE_STAIRS, Ingredient.ofItems(Moditems.Siderite)).criterion(hasItem(Moditems.Siderite),conditionsFromItem(Moditems.Siderite)).offerTo(exporter, new Identifier(getRecipeName(ModBlocks.SIDERITE_TRAPDOOR)));
ShapelessRecipeJsonBuilder.create(RecipeCategory.REDSTONE,ModBlocks.SIDERITE_BUTTON,1)
.input(Moditems.Siderite)
.criterion("has Siderite" , InventoryChangedCriterion.Conditions.items(Moditems.Siderite))
.offerTo(exporter, new Identifier(getRecipeName(ModBlocks.SIDERITE_BUTTON)));
}
}
這裡很多事之後會提到的非正方體方塊類的生成,我們先看第一個
offerReversibleCompactingRecipes(exporter, RecipeCategory.BUILDING_BLOCKS, Moditems.Siderite, RecipeCategory.MISC, ModBlocks.SIDERITE_BLOCK);
offerReversibleCompactingRecipes提供了可以逆壓縮的配方,就跟鐵錠跟鐵磚的合成表一樣,是可以以9:1的比例互相轉換的。
參數中exporter負責接收生成的配方,並在遊戲中讀取與使用。
RecipeCategory.BUILDING_BLOCKS代表合成出的方塊的分類,Moditems.Siderite代表我們的壓縮原材料,RecipeCategory.MISC這個欄位代表原材料的分類為雜項,ModBlocks.SIDERITE_BLOCK則是最後的產出物